package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.Teachercourse1;
import com.woniu.pojo.*;
import com.woniu.pojo.Class;
import com.woniu.service.ClassService;
import com.woniu.service.CourseService;
import com.woniu.service.EmpService;
import com.woniu.service.TeacherCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("teacherCourse")
public class TeacherCourseController {
    @Autowired
    TeacherCourseService teacherCourseService;
    @Autowired
    ClassService classService;
    @Autowired
    EmpService empService;
    @Autowired
    CourseService courseService;
    /**
     * 查询讲师所带班级
     * */
    @RequestMapping("findClass")
    @ResponseBody
    public List<Teachercourse1> findClass(Integer empId){
        List<Teachercourse1> teachercourses = teacherCourseService.findByTeacherId(empId);

        return teachercourses;
    }
    /**
     *
     *展示页面跳转
     */
    @RequestMapping("listJump")
    public String listJump(Model model){
        List<Course> courseList = courseService.selectAll();
        List<Class> classList = classService.selectAll();
        List<Emp> empList = empService.selectAll();
        model.addAttribute("courseList",courseList);
        model.addAttribute("classList",classList);
        model.addAttribute("empList",empList);

        return "WEB-INF/teachercourse/list";
    }
    /**
     *  增加页面跳转
     */
    @RequestMapping("addJump")
    public String addJump(Model model){
        List<Course> courseList = courseService.selectAll();
        List<Class> classList = classService.selectAll();
        List<Emp> empList = empService.selectAll();
        model.addAttribute("courseList",courseList);
        model.addAttribute("classList",classList);
        model.addAttribute("empList",empList);
        return "WEB-INF/teachercourse/add";
    }
    /**
     *  给老师安排课程数据
     */
    @RequestMapping("add")
    @ResponseBody
    public Map<String,Object> add(Teachercourse teachercourse){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            teacherCourseService.add(teachercourse);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    /**
     *
     取消给老师安排的课
     */
    @RequestMapping("deleteById")
    @ResponseBody
    public Map<String,Object> deleteById(Integer coursetId){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            teacherCourseService.deleteById(coursetId);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }

    /**
     *查找老师课程信息
     */
    @RequestMapping("findOne")
    @ResponseBody
    public Map<String, Object> findOne(Integer coursetId){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            Teachercourse teachercourse =teacherCourseService.selectOne(coursetId);
            map.put("teachercourse",teachercourse);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    /**
     *  跳转到修改
     */
    @RequestMapping("updateJump")
    public String updateJump(Model model, Integer coursetId){
        Teachercourse teachercourse =teacherCourseService.selectOne(coursetId);
        List<Course> courseList = courseService.selectAll();
        List<Class> classList = classService.selectAll();
        List<Emp> empList = empService.selectAll();
        model.addAttribute("courseList",courseList);
        model.addAttribute("classList",classList);
        model.addAttribute("empList",empList);
        model.addAttribute("teachercourse",teachercourse);
        return "WEB-INF/teachercourse/update";
    }
    /**
     *  给老师调课
     */
    @RequestMapping("updateById")
    @ResponseBody
    public Map<String, Object> updateById(Teachercourse teachercourse){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            teacherCourseService.updateById(teachercourse);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }

    /**
     *茶道所有的老师课程信息
     */
    @RequestMapping("findAll")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="6") Integer pageSize){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Teachercourse1> teachercourseList=teacherCourseService.findAll();
            PageInfo<Teachercourse1> pageInfo=new PageInfo<Teachercourse1>(teachercourseList);
            map.put("pageInfo",pageInfo);
            map.put("teachercourseList",teachercourseList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }

    /**
     * 批量取消老师的课程
     */
    @RequestMapping("deleteByExample")
    @ResponseBody
    public Map<String,Object> deleteByExample(Integer[] coursetIds){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            teacherCourseService.deleteByExample(Arrays.asList(coursetIds));
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }

    /**
     根据条件查找老师课程数据
     */
    @RequestMapping("findDate")
    @ResponseBody
    public Map<String, Object> findDate( Teachercourse teachercourse, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="6") Integer pageSize){
        Map<String, Object> map=new HashMap<String,Object>();
        //接收的查询条件放入map
        Map<String, Object> mapCondition=new HashMap<String,Object>();
        mapCondition.put("teachercourse",teachercourse);
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Teachercourse1> teachercourseList=teacherCourseService.findDate(mapCondition);
            for (Teachercourse1 t:teachercourseList
                 ) {
                System.out.println(t);
            }
            PageInfo<Teachercourse1> pageInfo=new PageInfo<Teachercourse1>(teachercourseList);
            map.put("pageInfo",pageInfo);
            map.put("teachercourseList",teachercourseList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
}
